<template>
  <div class="box" style="height: 500px">
    <div class="xtx-slider" @mouseenter="mouseenter" @mouseleave="mouseleave">
      <!-- 图片列表 -->
      <ul class="slider-body">
        <!--
          fade: 当fade类名存在 当前图片就显示 不存在就不显示
         -->
        <li
          class="slider-item"
          :class="{ fade: curIndex === index }"
          v-for="(item, index) in sliders"
          :key="item.id"
        >
          <img :src="item.imgUrl" />
        </li>
      </ul>
      <!-- 圆圈切换按钮 -->
      <div class="slider-indicator">
        <span
          :class="{ active: curIndex === index }"
          v-for="(item, index) in sliders"
          :key="item.id"
          @click="SetcurIndex(index)"
        ></span>
      </div>

      <div class="next" @click="next"><span class="iconfont icon-angle-right"></span></div>
      <div class="prev" @click="prev"><span class="iconfont icon-angle-left"></span></div>
    </div>
  </div>
</template>

<script>
/*
  (tab切换类交互)
  1. 点击圆圈 切换图片
    思路分析：
      1. 图片个数和小圆圈图标个数是一致 下标值一一对应
      2. 谁身上有名称为fade类名就会得到显示
      3. 核心：点击圆圈记录当前激活下标  然后通过li自己的下标index匹配下标 如果发生匹配就显示fade 否则不显示fade

  2. 自动播放
    思路分析：
      1. curIdx的变化会导致fade类型切换 从而让图片切换
      2. 自动播放就是从之前点击切换curIdx 变成让它自己变  (++)

  3. 优化
      1. 组件卸载时清除定时器
      2. 鼠标移入的时候停止 鼠标移除再次开启

  props
    1. 依赖的图片列表数据
    2. 自动轮播的时间
    3. 是否开启自动轮播

*/
import { onMounted, onUnmounted, ref } from 'vue'

export default {
  name: 'XtxSlider',
  props: {
    sliders: {
      type: Array,
      default: () => {
        return []
      }
    },
    data: {
      type: Number,
      default: 2000
    },
    isAuto: {
      type: Boolean,
      default: true
    }
  },
  setup (props) {
    const curIndex = ref(0)

    function SetcurIndex (index) {
      curIndex.value = index
    }
    let timer = null
    function startTimer () {
      if (!props.isAuto) {
        return
      }
      timer = setInterval(() => {
        curIndex.value++
        if (curIndex.value === props.sliders.length) {
          curIndex.value = 0
        }
      }, props.data)
    }
    function stopTimer () {
      clearInterval(timer)
    }
    function mouseleave () {
      startTimer()
    }
    function mouseenter () {
      stopTimer()
    }
    function next () {
      curIndex.value++
      if (curIndex.value === props.sliders.length) {
        curIndex.value = 0
      }
    }
    function prev () {
      curIndex.value--
      if (curIndex.value < 0) {
        curIndex.value = props.sliders.length - 1
      }
    }
    onMounted(() => {
      startTimer()
    })
    onUnmounted(() => {
      stopTimer()
    })
    return {
      curIndex,
      SetcurIndex,
      startTimer,
      stopTimer,
      mouseleave,
      mouseenter,
      next,
      prev
    }
  }
}
</script>

<style scoped lang='less'>
.xtx-slider {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .slider {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .slider-btn {
      opacity: 1;
    }
  }
  .next {
    width: 50px;
    height: 50px;
    background-color: #a5a2a2;
    opacity: 0.7;
    border-radius: 50%;
    position: absolute;
    top: 230px;
    right: 12px;
    z-index: 1;
    span {
      font-size: 30px;
      position: absolute;
      top: 3px;
      left: 11px;
      font-weight: 700;
    }
  }
   .prev {
    width: 50px;
    height: 50px;
   background-color: #a5a2a2;
    opacity: 0.7;
    border-radius: 50%;
    position: absolute;
    top: 230px;
    left: 280px;
    z-index: 1;
    span {
      font-size: 30px;
      position: absolute;
      top: 3px;
      left: 7px;
      font-weight: 700;
    }
  }
}
</style>
